package com.twetec.cms.adapter.pc.backend.config;

import com.twetec.cms.adapter.pc.backend.interceptor.AuthenticationInterceptor;
import com.twetec.cms.adapter.pc.backend.interceptor.AuthorizationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 配置-springmvc
 */
@Configuration
public class SpringMvcConfig implements WebMvcConfigurer {

    /**
     * 身份验证路由-拦截
     */
    private static final String[] AUTHENTICATION_INCLUDE_PATH_PATTERNS = {
            "/**"
    };

    /**
     * 身份验证路由-放行
     */
    private static final String[] AUTHENTICATION_EXCLUDE_PATH_PATTERNS = {
            "/swagger-resources/**",
            "/swagger-ui.html/**",
            "/webjars/**",
            "/v2/**",

            "/ucenter/auth/user-login",
//            "/**",
            //TODO:: 打包要注释  "/**",
            "/test/**"
    };

    /**
     * 权限验证路由-拦截
     */
    private static final String[] AUTHORIZATION_INCLUDE_PATH_PATTERNS = {
            "/**"
    };
    //TODO:: 打包要解开注释

    /**
     * 权限验证路由-放行
     */
    private static final String[] AUTHORIZATION_EXCLUDE_PATH_PATTERNS = {
            "/ucenter/auth/user-login",
            "/swagger-resources/**",
            "/swagger-ui.html/**",
            "/webjars/**",
            "/v2/**",

            // 用户鉴权
            "/ucenter/auth/get-user-auth-list", // 用户权限查询
            "/ucenter/auth/get-user-info", // 用户信息查询
            "/ucenter/auth/user-login", // 用户登录
    };

    /**
     * 拦截器-用户身份
     */
    @Autowired
    private AuthenticationInterceptor authenticationInterceptor;

    /**
     * 拦截器-用户权限
     */
    // TODO:: 权限拦截器待处理
//    @Autowired
//    private AuthorizationInterceptor authorizationInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 用户身份
        registry.addInterceptor(authenticationInterceptor)
                .addPathPatterns(AUTHENTICATION_INCLUDE_PATH_PATTERNS)
                .excludePathPatterns(AUTHENTICATION_EXCLUDE_PATH_PATTERNS);

        // TODO:: 权限拦截器待处理
        // 用户权限
//        registry.addInterceptor(authorizationInterceptor)
//                .addPathPatterns(AUTHORIZATION_INCLUDE_PATH_PATTERNS)
//                .excludePathPatterns(AUTHORIZATION_EXCLUDE_PATH_PATTERNS);

    }

}
